<form id="submit-form" class="layui-form layui-card" action="{:request()->url()}" data-auto="true" method="post" autocomplete="off">
    <div class="layui-card-body">

        <div class="layui-form-item">
            <label class="layui-form-label">门店</label>
            <div class="layui-input-block">
                <select name="shop_id" lay-filter="distributor" lay-search required>
                    <option value="">请选择</option>
                    {volist name="distributor" id="v"}
                    <option value="{$key}" {if isset($vo.shop_id) && $key== $vo.shop_id} selected {/if}>{$v}</option>
                    {/volist}
                </select>
                <p class="help-block"></p>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">设备</label>
            <div class="layui-input-block">
                <select name="equip_id" lay-filter="equip" lay-search required>
                    <option value="">请选择</option>
                    {volist name="equip" id="v"}
                    <option value="{$key}" {if isset($vo.equip_id) && $key== $vo.equip_id} selected {/if}>{$v}</option>
                    {/volist}
                </select>
                <p class="help-block"></p>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">通道</label>
            <div class="layui-input-block">
                <select name="channel_id" lay-filter="channel" lay-search required>
                    <option value="">请选择</option>
                </select>
                <p class="help-block"></p>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">存储地址</label>
            <div class="layui-input-block">
                <input name="storage_path" value='{$vo.storage_path|default=""}' required placeholder="请输入存储地址"
                       class="layui-input">
                <p class="help-block"></p>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入备注" name="remarks" class="layui-textarea">{$vo.remarks|default=""}</textarea>
                <p class="help-block"></p>
            </div>
        </div>
    </div>

    <div class="hr-line-dashed"></div>
    {notempty name='vo.id'}<input type='hidden' value='{$vo.id}' name='id'>{/notempty}

    <div class="layui-form-item text-center">
        <button class="layui-btn" type='submit' id="submitBtn">保存数据</button>
        <button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消编辑吗？" data-close>取消编辑</button>
    </div>
</form>
<script>
    form.render();

    $('#submitBtn').click(function () {
        let shop_id = $('#submit-form select[name="shop_id"]').val();
        if (!shop_id) {
            layer.msg('请选择门店', {time: 1000});
            return false;
        }
        let equip_id = $('#submit-form select[name="equip_id"]').val();
        if (!equip_id) {
            layer.msg('请选择设备', {time: 1000});
            return false;
        }
        let channel_id = $('#submit-form select[name="channel_id"]').val();
        if (!channel_id) {
            layer.msg('请选择通道', {time: 1000});
            return false;
        }
    })

    form.on('select(equip)', function (data) {
        let pid = data.value;

        $.ajax({
            url: "{:url('cloud_storage/channelArray')}",
            data: {'pid': pid},
            success: function (result) {
                let data = result.data;
                let selelt = $('select[name="channel_id"]');
                selelt.html('<option value="">请选择</option>')
                $.each(data, function (index, value) {
                    // console.log(value.department_id);
                    selelt.append(new Option(value, index));// 下拉菜单里添加元素
                });
                form.render("select");//重新渲染 固定写法
            }
        });
        $('select[name="channel_id"]').html('<option value="">请选择</option>')
    })

    channel();

    function channel() {
        let pid = "{$vo['equip_id'] ?? 0}";
        let id = "{$vo['channel_id'] ?? 0}";
        $.ajax({
            url: "{:url('cloud_storage/channelArray')}",
            data: {'pid': pid},
            success: function (result) {
                let data = result.data;
                let selelt = $('select[name="channel_id"]');
                selelt.html('<option value="">请选择</option>')
                $.each(data, function (index, value) {
                    // console.log(value.department_id);
                    if (id == index) {
                        selelt.append('<option value="' + index + '" selected>' + value + '</option>')
                    } else {
                        selelt.append(new Option(value, index));// 下拉菜单里添加元素
                    }
                });
                form.render("select");//重新渲染 固定写法
            }
        });
    }

</script>



